#!/bin/bash

export PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"

ip=192.168.199.68
mip=172.16.199.68

Check_OS() {
	if [ -n "$(grep 'Aliyun Linux release' /etc/issue)" -o -e /etc/redhat-release ]; then
		OS=CentOS
		[ -n "$(grep ' 7\.' /etc/redhat-release 2> /dev/null)" ] && CentOS_RHEL_version=7
		[ -n "$(grep ' 6\.' /etc/redhat-release 2> /dev/null)" -o -n "$(grep 'Aliyun Linux release6 15' /etc/issue)" ] && CentOS_RHEL_version=6
		[ -n "$(grep ' 5\.' /etc/redhat-release 2> /dev/null)" -o -n "$(grep 'Aliyun Linux release5' /etc/issue)" ] && CentOS_RHEL_version=5
	elif [ -n "$(grep 'Amazon Linux AMI release' /etc/issue)" -o -e /etc/system-release ]; then
		OS=CentOS
		CentOS_RHEL_version=6
	fi
}
Check_OS
[ $CentOS_RHEL_version -ne 7 ] && echo "Only not support CentOS7" && exit
egrep '(vmx|svm)' /proc/cpuinfo
[ $? -ne 0 ] && echo "Hardware virtualization is not supported" &&exit 1
if ! which wget > /dev/null ;then
	yum install -y wget
fi
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum makecache
yum install -y net-tools vim lrzsz tree screen lsof tcpdump
systemctl disable firewalld
systemctl stop firewalld
systemctl disable NetworkManager
systemctl stop NetworkManager
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
grep SELINUX=disabled /etc/selinux/config
setenforce 0 
echo '  *  -  nofile   100000  '  >>/etc/security/limits.conf
yum update -y
hostnamectl --static set-hostname kvm-node1
cat > /etc/sysconfig/network-scripts/ifcfg-br0 <<EOF
TYPE="Bridge"
BOOTPROTO="static"
NAME="br0"
DEVICE="br0"
ONBOOT="yes"
IPADDR="${ip}"
NETMASK="255.255.255.0"
EOF

cat > /etc/sysconfig/network-scripts/ifcfg-br1 <<EOF
TYPE="Bridge"
BOOTPROTO="static"
NAME="br1"
DEVICE="br1"
ONBOOT="yes"
IPADDR="${nip}"
NETMASK="255.255.255.0"
EOF

mv /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0.bak
mv /etc/sysconfig/network-scripts/ifcfg-eth1 /etc/sysconfig/network-scripts/ifcfg-eth1.bak

cat > /etc/sysconfig/network-scripts/ifcfg-eth0 <<EOF
TYPE="Ethernet"
BOOTPROTO="none"
NAME="eth0"
BRIDGE="br0"
DEVICE="eth0"
ONBOOT="yes"
EOF

cat > /etc/sysconfig/network-scripts/ifcfg-eth1 <<EOF
TYPE="Ethernet"
BOOTPROTO="none"
NAME="eth1"
BRIDGE="br1"
DEVICE="eth1"
ONBOOT="yes"
EOF

yum -y install kvm
lsmod | grep kvm

yum -y install python-virtinst libvirt tunctl  bridge-utils virt-manager qemu-kvm-tools virt-viewer virt-install virt-v2v libguestfs-tools acpid

cat > /etc/sysconfig/grub <<EOF
GRUB_TIMEOUT=5
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto rhgb net.ifnames=0 biosdevname=0 quiet"
GRUB_DISABLE_RECOVERY="true"
EOF

grub2-mkconfig -o /boot/grub2/grub.cfg

[ $? -eq 0 ] && echo "Success,15s reboot." 
sleep 15
reboot